package com.lxg.config;

import com.lxg.service.MyDetailUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author 陆小根
 * date: 2022/04/25
 * Description:
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  private final MyDetailUserService myDetailUserService;

  @Autowired
  public WebSecurityConfig(MyDetailUserService myDetailUserService) {
    this.myDetailUserService = myDetailUserService;
  }

  // 使用PasswordEncoder 第二种方式   (使用固定密码加密方案)
//  @Bean
//  public PasswordEncoder passwordEncoder() {
//    return new BCryptPasswordEncoder();
//  }


//  @Bean
//  public UserDetailsService userDetailsService() {
//    InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
//    // 第一种 passwordEncoder 使用方式 灵活
////    inMemoryUserDetailsManager.createUser(User.withUsername("root").password("{bcrypt}$2a$16$123b9CO23ThuYqBuOpCP2Oq/324P/RikC.1ZFqHZXHzNiuxotjBIa").roles("admin").build());
//    inMemoryUserDetailsManager.createUser(User.withUsername("root").password("$2a$16$123b9CO23ThuYqBuOpCP2Oq/324P/RikC.1ZFqHZXHzNiuxotjBIa").roles("admin").build());
//    return inMemoryUserDetailsManager;
//  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(myDetailUserService);
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .csrf().disable()
    ;
  }
}
